﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Web_Oto.Bases;

namespace Web_Oto.Data
{
    public class UserDal : dalObject
    {
        #region constructors

        public UserDal(string connectionstring, string connectiontype)
            : base(connectionstring, connectiontype)
        {

        }

        public UserDal(IDbConnection sqlConnection, string connectiontype, SqlTransaction dbTransaction)
            : base(sqlConnection, connectiontype, dbTransaction)
        {

        }

        #endregion

        #region methods

        public IList<User> GetAllUser()
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.Users.Where(p => p.IsDeleted == false || p.IsDeleted == null).ToList();
            }
        }

        public User GetUserById(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.Users.FirstOrDefault(c => c.Id == id);
            }
        }

        public int Delete(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var user = db.Users.FirstOrDefault(p => p.Id == id);
                    if (user != null)
                    {
                        //db.Users.DeleteOnSubmit(user);
                        //db.SubmitChanges();
                        user.IsDeleted = true;
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public int Update(User user)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    if (db.Users.Any(p => p.UserName == user.UserName && p.Id != user.Id))
                        return -2;
                    var userDb = db.Users.FirstOrDefault(p => p.Id == user.Id);
                    if (userDb != null)
                    {
                        userDb.Name = user.Name;
                        userDb.UserName = user.UserName;
                        if (!string.IsNullOrEmpty(user.Password))
                            userDb.Password = user.Password;
                        userDb.Note = user.Note;
                        userDb.IsDeleted = false;
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public int Insert(User user)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                if (db.Users.Any(p => p.UserName == user.UserName))
                    return -2;

                var result = 1;
                try
                {
                    user.IsDeleted = false;
                    if (!string.IsNullOrEmpty(user.Password))
                        user.Password = user.Password;

                    db.Users.InsertOnSubmit(user);
                    db.SubmitChanges();
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public User GetUserByUserName(string userName)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.Users.FirstOrDefault(c => c.UserName == userName);
            }
        }

        public User CheckUserLogin(string userName, string password)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.Users.FirstOrDefault(c => c.UserName == userName && c.Password == password);
            }
        }

        #endregion
    }
}
